<?php


namespace api\channel\JdUnion;

use Dml\EasyTBK\JingDong\Request\JdUnionOrderRowQueryRequest;

/**
 * 统一拉取订单
 * Class openOrderRow
 * @package api\channel\JdUnion
 */
class openOrderRow extends Base
{

    //初始化
    public function __construct()
    {
        parent::__construct(PRODUCT_MOBILE_CPS_SERV);
    }

    /**
     * 错误代码示例
     * @var
     */
    public $codeMsg = [
        '2002408'   =>  '接口降级处理',
        '200'       =>  '调用成功',
        '2002400'   =>  '参数错误',
        '2002499'   =>  '数组类型参数元数个数，不符合业务规定(或List、分隔符串类型)',
        '2002401'   =>  'token不合法',
        '2002500'   =>  '系统异常，请稍后重试',
        '2002403'   =>  '无访问权限',
        '2002452'   =>  '单位时间内订单量超过最大值(10000),请缩小查询时间范围',
        '2002453'   =>  '无效请求-查询时间范围超过规定限制',
        '2002410'   =>  '联盟用户不存在，请检查unionId是否正确',
        '2002411'   =>  '注意key不可和childUnionid同时传入',
    ];

    public function orders($param)
    {
        $params = [
            'pageIndex' => $param['page'],
            'pageSize'  => 100,
            'type'      => 3,
            'startTime' => date('Y-m-d H:i:s',strtotime('-1 hour')),
            'endTime'   => date('Y-m-d H:i:s', time()),
        ];
        $result = $this->jd_union_open_order_row_query($params);

        $res['data'] = $result['data']['orderRowResp'] ?? [];
        return $res;
    }

    /**
     * Notes:查询订单
     * User: ymzhang
     * Date: 2022/08/01
     * Time: 09:10
     * @param array $params
     * @return array
     */
    public function jd_union_open_order_row_query(array $params)
    {
        if (isset($params['childUnionId']) && isset($params['key']) && !empty($params['childUnionId']) && !empty($params['key'])) {
            return web_request([],false, '2002411', $this->getMsg('2002411'));
        }
        $req = new JdUnionOrderRowQueryRequest();
        $req->setPageIndex($params['pageIndex']??1);
        $req->setPageSize($params['pageSize']??20);
        $req->setType($params['type']??3);
        $req->setStartTime($params['startTime']);
        $req->setEndTime($params['endTime']);
        if (isset($params['childUnionId'])) {
            $req->setChildUnionId($params['childUnionId']);
        }
        if (isset($params['key'])) {
            $req->setKey($params['key']);
        }
        if (isset($params['orderId'])) {
            $req->setOrderId($params['orderId']);
        }
        if (isset($params['fields'])) {
            $req->setOrderId($params['fields']);
        }

        $result = $this->jd->execute($req);
        if (isset($result['error_response'])) {
            return web_request([],false, $result['error_response']['code'], $result['error_response']['zh_desc']);
        }
        $queryResult = json_decode($result[$req->getOutputParameters()]['queryResult'], true);
        if ($code = $queryResult['code'] != 200) {
            return web_request([],false, $code, $this->getMsg($code));
        }
        //组装数据
        $data = [
            'orderRowResp' => $queryResult['data']??[],
            'hasMore' =>$queryResult['hasMore']??false,
        ];
        return web_request($data,true, 200, $this->getMsg('200'));
    }




}